package top.fengyulou.xiaofeng.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import top.fengyulou.xiaofeng.common.MyUtils;
import top.fengyulou.xiaofeng.common.UuidUtil;
import top.fengyulou.xiaofeng.domain.Paper;
import top.fengyulou.xiaofeng.domain.PaperQuestion;
import top.fengyulou.xiaofeng.domain.PaperQuestionExample;
import top.fengyulou.xiaofeng.dto.*;
import top.fengyulou.xiaofeng.enums.StatusEnum;
import top.fengyulou.xiaofeng.mapper.*;
import top.fengyulou.xiaofeng.mapper.my.MyPaperMapper;
import top.fengyulou.xiaofeng.mapper.my.MyQuestionMapper;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @ClassName PaperService
 * @Description TODO
 * @Author qwer
 * @Date 2021/8/22 1:54
 * @Version 1.0
 */
@Service
public class PaperQuestionService {

    @Resource
    private PaperMapper paperMapper;
    @Resource
    private PaperQuestionMapper paperQuestionMapper;
    @Resource
    private MyPaperMapper myPaperMapper;
    @Resource
    private PaperUserMapper paperUserMapper;
    @Resource
    private QuestionOptionMapper questionOptionMapper;
    @Resource
    private MyQuestionMapper myQuestionMapper;
    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private PaperService paperService;

    /**
     * 列表查询
     */
    public void listPage(PaperQuestionDto query) {
        //查询数据
        PageHelper.startPage(query.getPageNumber(), query.getPageSize());
        List<PaperQuestionDto> list = paperQuestionMapper.list(query);
        PageInfo<PaperQuestionDto> pageInfo = new PageInfo<>(list);

        //返回结果
        query.setTotal(pageInfo.getTotal());
        query.setRows(list);
    }

    /**
     * 保存试题
     *
     * @param paperQuestionDto
     */
    @Transactional
    public void save(PaperQuestionDto paperQuestionDto) {
        if (StringUtils.isNotBlank(paperQuestionDto.getId())){
            paperQuestionDto.setUpdateTime(new Date());
            paperQuestionDto.setLastUpdateUserId(paperQuestionDto.getCurrentUserId());
            paperQuestionMapper.update(paperQuestionDto);
        }else {
            Date date = new Date();
            List<PaperQuestionDto> list = new ArrayList<>();
            for (String qstId : paperQuestionDto.getQstIds()) {
                PaperQuestionDto db = new PaperQuestionDto();
                db.setId(UuidUtil.getShortUuid());
                db.setPaperId(paperQuestionDto.getPaperId());
                db.setQuestionId(qstId);
                db.setCreateUserId(paperQuestionDto.getCurrentUserId());
                db.setCreateTime(date);
                db.setQstType(paperQuestionDto.getQstType());
                list.add(db);
            }
            paperQuestionMapper.insertBatch(list);

            //修改试卷试题数量
            PaperDto paperDto = new PaperDto();
            paperDto.setId(paperQuestionDto.getPaperId());
            paperDto.setCurrentUserId(paperQuestionDto.getCurrentUserId());
            paperService.updateQstNum(paperDto);
        }
    }

    @Transactional
    public void updateScore(PaperQuestionDto paperQuestionDto) {
        //修改试卷试题分数
        paperQuestionDto.setUpdateTime(new Date());
        paperQuestionDto.setLastUpdateUserId(paperQuestionDto.getCurrentUserId());
        paperQuestionMapper.updateScore(paperQuestionDto);
        //修改试卷总分数
        PaperDto paperDto = new PaperDto();
        paperDto.setId(paperQuestionDto.getPaperId());
        paperDto.setCurrentUserId(paperQuestionDto.getCurrentUserId());
        paperService.updateQstScore(paperDto);
    }

    public void qstListPage(QuestionDto query) {
        //查询数据
        PageHelper.startPage(query.getPageNumber(), query.getPageSize());
        List<QuestionDto> list = paperQuestionMapper.qstList(query);
        PageInfo<QuestionDto> pageInfo = new PageInfo<>(list);

        //返回结果
        query.setTotal(pageInfo.getTotal());
        query.setRows(list);
    }

    //删除
    public void delete(String[] ids) {
        paperQuestionMapper.delete(ids);
    }

    //删除
    @Transactional
    public void deleteExtend(PaperQuestionDto paperQuestionDto) {
        //删除试卷试题
        paperQuestionMapper.delete(paperQuestionDto.getIds());
        //修改试卷试题和试卷总分数
        PaperDto paperDto = new PaperDto();
        paperDto.setId(paperQuestionDto.getPaperId());
        paperDto.setCurrentUserId(paperQuestionDto.getCurrentUserId());
        paperService.updateQstNum(paperDto);
        paperService.updateQstScore(paperDto);
    }

    //根据id查询
    public PaperQuestionDto getByPaperIdAndQstId(String paperId,String qstId) {
        return paperQuestionMapper.getByPaperIdAndQstId(paperId,qstId);
    }
}

